home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / communicator / pref / pref-applications-new.js < prev    next >
Encoding:
JavaScript  |  2002-05-13  |  6.2 KB  |  182 lines

  1. /* -*- Mode: Java; tab-width: 2; c-basic-offset: 2; -*-
  2.  * 
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  *
  18.  * Contributor(s):
  19.  */
  20.  
  21. var gDescriptionField = null;
  22. var gExtensionField   = null;
  23. var gMIMEField        = null;
  24. var gAppPath          = null;
  25.  
  26. var gPrefApplicationsBundle = null;
  27.  
  28. function Startup()
  29. {
  30.   doSetOKCancel(onOK);
  31.   
  32.   gDescriptionField = document.getElementById("description");
  33.   gExtensionField   = document.getElementById("extensions");
  34.   gMIMEField        = document.getElementById("mimeType");
  35.   gAppPath          = document.getElementById("appPath");
  36.     
  37.   gPrefApplicationsBundle = document.getElementById("bundle_prefApplications");
  38.  
  39.   // If an arg was passed, then it's an nsIHelperAppLauncherDialog
  40.   if ( "arguments" in window && window.arguments[0] ) {
  41.       // Get mime info.
  42.       var info = window.arguments[0].mLauncher.MIMEInfo;
  43.  
  44.       // Fill the fields we can from this.
  45.       gDescriptionField.value = info.Description;
  46.       gExtensionField.value   = info.primaryExtension;
  47.       gMIMEField.value        = info.MIMEType;
  48.       // an app may have been selected in the opening dialog but not in the mimeinfo
  49.       var app = info.preferredApplicationHandler || window.arguments[0].chosenApp;
  50.       if ( app ) {
  51.           gAppPath.value      = app.path;
  52.       }
  53.  
  54.       // Don't let user change mime type.
  55.       gMIMEField.setAttribute( "readonly", "true" );
  56.  
  57.       // Start user in app field.
  58.       gAppPath.focus();
  59.   } else {
  60.       gDescriptionField.focus();
  61.   }
  62.   sizeToContent();
  63.   moveToAlertPosition();
  64. }
  65.  
  66. function chooseApp()
  67. {
  68.   const nsIFilePicker = Components.interfaces.nsIFilePicker;
  69.   var filePicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  70.   if (filePicker) {
  71.     const FP = Components.interfaces.nsIFilePicker
  72.     var windowTitle = gPrefApplicationsBundle.getString("chooseHandler");
  73.     var programsFilter = gPrefApplicationsBundle.getString("programsFilter");
  74.     filePicker.init(window, windowTitle, FP.modeOpen);
  75.     if (navigator.platform == "Win32")
  76.       filePicker.appendFilter(programsFilter, "*.exe; *.com");
  77.     else
  78.       filePicker.appendFilters(FP.filterAll);
  79.     var filePicked = filePicker.show();
  80.     if (filePicked == nsIFilePicker.returnOK && filePicker.file) {
  81.       var file = filePicker.file.QueryInterface(Components.interfaces.nsILocalFile);
  82.       gAppPath.value = file.path;
  83.       gAppPath.select();
  84.     }
  85.   }
  86. }
  87.  
  88. var gDS = null;
  89. function onOK()
  90. {
  91.   // Make sure all fields are filled in OK.
  92.   if ( !checkInput() ) {
  93.     return false;
  94.   }
  95.  
  96.   const mimeTypes = "UMimTyp";
  97.   var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
  98.   
  99.   var file = fileLocator.get(mimeTypes, Components.interfaces.nsIFile);
  100.   
  101.   var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  102.   
  103.   gDS = gRDF.GetDataSource(ioService.getURLSpecFromFile(file));
  104.  
  105.   gMIMEField.value = gMIMEField.value.toLowerCase();
  106.     
  107.   // figure out if this mime type already exists. 
  108.   var exists = mimeHandlerExists(gMIMEField.value);
  109.   if (exists) {
  110.     var titleMsg = gPrefApplicationsBundle.getString("handlerExistsTitle");
  111.     var dialogMsg = gPrefApplicationsBundle.getString("handlerExists");
  112.     dialogMsg = dialogMsg.replace(/%mime%/g, gMIMEField.value);
  113.     var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  114.     var replace = promptService.confirm(window, titleMsg, dialogMsg);
  115.     if (!replace)
  116.     {
  117.       window.close();
  118.       return;
  119.     }
  120.   }
  121.   
  122.   
  123.   // now save the information
  124.   var handlerInfo = new HandlerOverride(MIME_URI(gMIMEField.value));
  125.   handlerInfo.mUpdateMode = exists; // XXX Somewhat sleazy, I know...
  126.   handlerInfo.mimeType = gMIMEField.value;
  127.   handlerInfo.description = gDescriptionField.value;
  128.   
  129.   var extensionString = gExtensionField.value.replace(/[*.;]/g, "").toLowerCase();
  130.   var extensions = extensionString.split(" ");
  131.   for (var i = 0; i < extensions.length; i++) {
  132.     var currExtension = extensions[i];
  133.     handlerInfo.addExtension(currExtension);
  134.   }
  135.   handlerInfo.appPath = gAppPath.value;
  136.  
  137.   // other info we need to set (not reflected in UI)
  138.   handlerInfo.isEditable = true;
  139.   handlerInfo.saveToDisk = false;
  140.   handlerInfo.handleInternal = false;
  141.   handlerInfo.alwaysAsk = true;
  142.   file = Components.classes["@mozilla.org/file/local;1"].createInstance();
  143.   if (file)
  144.     file = file.QueryInterface(Components.interfaces.nsILocalFile);
  145.   if (file) {
  146.     try {
  147.       file.initWithPath(gAppPath.value);
  148.       handlerInfo.appDisplayName = file.leafName;
  149.     }
  150.     catch(e) {
  151.       handlerInfo.appDisplayName = gAppPath.value;    
  152.     }
  153.   }
  154.   // do the rest of the work (ugly yes, but it works)
  155.   handlerInfo.buildLinks();
  156.   
  157.   // flush the ds to disk.   
  158.   var remoteDS = gDS.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  159.   if (remoteDS)
  160.     remoteDS.Flush();
  161.   
  162.   // If an arg was passed, then it's an nsIHelperAppLauncherDialog
  163.   // and we need to update its MIMEInfo.
  164.   if ( "arguments" in window && window.arguments[0] ) {
  165.       // Get mime info.
  166.       var info = window.arguments[0].mLauncher.MIMEInfo;
  167.  
  168.       // Update fields that might have changed.
  169.       info.preferredAction = Components.interfaces.nsIMIMEInfo.useHelperApp;
  170.       info.Description = gDescriptionField.value;
  171.       info.preferredApplicationHandler = file;
  172.       info.applicationDescription = handlerInfo.appDisplayName;
  173.  
  174.       // Tell the nsIHelperAppLauncherDialog to update to the changes
  175.       window.arguments[0].updateSelf = true;
  176.   }
  177.  
  178.   window.opener.gNewTypeRV = gMIMEField.value;
  179.   window.close();  
  180. }
  181.  
  182.